package com.hmkj.core.mapper.account;

import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.core.model.account.AccountModel;
import com.hmkj.core.po.account.Account;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 用户账户表相关Dao接口<br>
 *
 * @author lizhijie
 * @date 2018-07-05 06:18:03
 */
public interface AccountMapper extends GenericMapper<Account, Integer> {

    /**
     * 用户账户列表
     *
     * @param params
     * @return
     */
    @Select("SELECT t.*,u.user_name AS 'u.userName',u.mobile_phone AS 'u.mobilePhone' FROM biz_account t LEFT JOIN biz_user u ON u.id = t.user_id WHERE 1 = 1 ${whereSql} ${sortSql} limit ${nowPage}, ${pageSize}")
    List<AccountModel> findListBySqlJoin(Map<String, Object> params);

    /**
     * 用户统计
     *
     * @param params
     * @return
     */
    @Select("SELECT count(1) FROM biz_account t LEFT JOIN biz_user u ON u.id = t.user_id WHERE 1 = 1 ${whereSql}")
    long countBySqlJoin(Map<String, Object> params);

    /**
     * 根据用户ID 查询用户账户信息
     *
     * @param userId 用户ID
     * @return
     */
    @Select("SELECT * FROM biz_account WHERE user_id = #{userId} and del_flag = 1")
    Account selectByUID(@Param(value = "userId") int userId);

    /**
     * 根据用户ID 更新用户模拟账户资金
     *
     * @param total          总额变化量
     * @param moneyAvailable 钱包金额变化量
     * @param available      可用变化量
     * @param freeze         冻结变化量
     * @param tenderCount    点买总额变化量
     * @param feeCount       费用总额变化量
     * @param profitCount    盈利总额变化量
     * @param userId         用户ID
     */
    @Update("UPDATE biz_account SET " +
            "total = total + (#{total}), " +
            "available = available + (#{available}), " +
            "money_available = money_available + (#{moneyAvailable}), " +
            "freeze = freeze + (#{freeze})," +
            "tender_count = tender_count + (#{tenderCount})," +
            "fee_count = fee_count + (#{feeCount})," +
            "profit_count = profit_count + (#{profitCount})," +
            "update_time = now() WHERE  user_id = #{userId} " +
            "AND total + (#{total}) >= 0 " +
            "AND money_available + (#{moneyAvailable}) >= 0 " +
            "AND available + (#{available}) >= 0 " +
            "AND freeze + (#{freeze}) >= 0 " +
            "AND tender_count + (#{tenderCount}) >= 0 " +
            "AND fee_count + (#{feeCount}) >= 0 " +
            "AND profit_count + (#{profitCount}) >= 0 ")
    int updateAccount(@Param(value = "total") BigDecimal total,
                      @Param(value = "moneyAvailable") BigDecimal moneyAvailable,
                      @Param(value = "available") BigDecimal available,
                      @Param(value = "freeze") BigDecimal freeze,
                      @Param(value = "tenderCount") BigDecimal tenderCount,
                      @Param(value = "feeCount") BigDecimal feeCount,
                      @Param(value = "profitCount") BigDecimal profitCount,
                      @Param(value = "userId") Integer userId);

    /**
     * 根据用户ID 更新平台模拟账户资金（不校验金额正负）
     *
     * @param total          总额变化量
     * @param moneyAvailable 钱包金额变化量
     * @param available      可用变化量
     * @param freeze         冻结变化量
     * @param tenderCount    点买总额变化量
     * @param feeCount       费用总额变化量
     * @param profitCount    盈利总额变化量
     * @param userId         用户ID
     */
    @Update("UPDATE biz_account SET" +
            "total = total + (#{total}), " +
            "money_available = money_available + (#{moneyAvailable}), " +
            "available = available + (#{available}), " +
            "freeze = freeze + (#{freeze})," +
            "tender_count = tender_count + (#{tenderCount})," +
            "fee_count = fee_count + (#{feeCount})," +
            "profit_count = profit_count + (#{profitCount})," +
            "update_time = now() WHERE  user_id = #{userId}")
    int updateAccount4Plat(@Param(value = "total") BigDecimal total,
                           @Param(value = "moneyAvailable") BigDecimal moneyAvailable,
                           @Param(value = "available") BigDecimal available,
                           @Param(value = "freeze") BigDecimal freeze,
                           @Param(value = "tenderCount") BigDecimal tenderCount,
                           @Param(value = "feeCount") BigDecimal feeCount,
                           @Param(value = "profitCount") BigDecimal profitCount,
                           @Param(value = "userId") Integer userId);
}
